package com.controller.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.service.QrcodeService;

@Controller
@RequestMapping("/qrcode")
public class QrcodeController {

	@Resource
	private QrcodeService qrcodeService;

	@RequestMapping(value = "/toQrcodePage")
	public ModelAndView toQrcodePage(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/qrcode");
	}

	@RequestMapping(value = "/addQrcodeInfo", method = RequestMethod.POST)
	@ResponseBody
	public String addQrcodeInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "pxtimg", required = false) MultipartFile[] pxtimg,
			@RequestParam(value = "jxtimg", required = false) MultipartFile[] jxtimg) {
		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://" + "www.baodaoapp.com"// 服务器地址
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			System.out.println("pathString:" + pathString);
			paramsMap.put("pathString", pathString);
			int result = qrcodeService.addQrcodeInfo(paramsMap, pxtimg, jxtimg);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/getQrcodeInfo", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getQrcodeInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> list = qrcodeService.getQrcodeInfoByUuid(paramsMap);
		request.setAttribute("qrcode", list.get(0));
		return new ModelAndView("/manager/indexAll");
	}

	@RequestMapping(value = "/getQrcode", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getQrcode(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		List<Map<String, String>> list = qrcodeService.getQrcodeInfoByUuid(paramsMap);
		request.setAttribute("qrcode", list.get(0));
		return new ModelAndView("/manager/index_two");
	}

	@RequestMapping(value = "/getQrcodeByParam", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getQrcodeByParam(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = qrcodeService.getQrcodeInfo(paramsMap);
			System.out.println(list);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/deleteQrcodeInfo", method = RequestMethod.POST)
	@ResponseBody
	public String deleteQrcodeInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = qrcodeService.deleteQrcodeInfo(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateQrcodeInfo", method = RequestMethod.POST)
	@ResponseBody
	public String updateQrcodeInfo(HttpServletRequest request, @RequestParam Map<String, String> paramsMap,
			@RequestParam(value = "pxtimg", required = false) MultipartFile[] pxtimg,
			@RequestParam(value = "jxtimg", required = false) MultipartFile[] jxtimg) {
		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://" + "www.baodaoapp.com"// 服务器地址
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			paramsMap.put("pathString", pathString);
			System.out.println("pathString:" + pathString);
			int result = qrcodeService.updateQrcodeInfo(paramsMap, pxtimg, jxtimg);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}
}
